package com.mdd.admin.service.coupon.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.coupon.ICouponReceiveLogService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.vo.coupon.CouponReceiveLogListVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.coupon.CouponReceiveLog;
import com.mdd.common.mapper.coupon.CouponReceiveLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class CouponReceiveLogServiceImpl implements ICouponReceiveLogService {

    @Resource
    private CouponReceiveLogMapper couponReceiveLogMapper;

    @Override
    public PageResult<CouponReceiveLogListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer pageNo = pageParam.getPageNo();
        Integer pageSize = pageParam.getPageSize();
        MPJQueryWrapper<CouponReceiveLog> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.select("t.id,t.receiveMethod,t.operator,t.receiveTime,t.expirationTime,t.useTime,t.status"+
                        "u.avatar as avatar, u.username as username, c.name as name")
                .innerJoin("?_user u ON u.id = t.uid".replace("?_", GlobalConfig.tablePrefix))
                .innerJoin("?_coupon c ON c.id = t.cid".replace("?_", GlobalConfig.tablePrefix))
                .orderByDesc("t.id");
        couponReceiveLogMapper.setSearch(mpjQueryWrapper, params, new String[]{
                "like:username@u.username:str",
                "like:name@c.name:str",
                "=:receiveMethod@t.receive_method:int",
                "=:operator@t.operator:int",
                "datetime:startTime-endTime@t.receive_time:str"
        });
        IPage<CouponReceiveLogListVo> iPage = couponReceiveLogMapper.selectJoinPage(
                new Page<>(pageNo, pageSize),
                CouponReceiveLogListVo.class,
                mpjQueryWrapper);
        iPage.getRecords().forEach( item -> {
            item.setCancelBtn(item.getStatus() == 0 ? 1 : 0);
            item.setOperatorDesc(item.getOperator());
            item.setReceiveMethodDesc(item.getReceiveMethod());
            item.setReceiveTime(item.getReceiveTime());
            item.setExpirationTime(item.getExpirationTime());
            item.setUseTime(item.getUseTime());
        });
        long all = iPage.getTotal();
        long unused = iPage.getRecords().stream().filter(x -> x.getStatus() == 0).distinct().count();
        long used = iPage.getRecords().stream().filter(x -> x.getStatus() == 1).distinct().count();
        long exp = iPage.getRecords().stream().filter(x -> x.getStatus() == 2).distinct().count();
        long cancel = iPage.getRecords().stream().filter(x -> x.getStatus() == 3).distinct().count();
        Map<String, Long> map = new LinkedHashMap<>();
        map.put("all",all);
        map.put("unused",unused);
        map.put("used",used);
        map.put("exp",exp);
        map.put("cancel", cancel);
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords(), map);
    }
}
